package com.ly.emp.controller.checkIn;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.emp.bean.dto.HubTeamDTO;
import com.ly.emp.bean.dto.TeamResultOrderDTO;
import com.ly.emp.bean.vo.HubTeamWorkVO;
import com.ly.emp.bean.vo.HubTreeVO;
import com.ly.emp.entity.HubTeamWork;
import com.ly.emp.entity.TeamResultOrder;
import com.ly.emp.entity.TeamResultOrderDetail;
import com.ly.emp.excelListener.HubTeamResultOrderExcelVO;
import com.ly.emp.excelListener.HubTeamResultOrderListener;
import com.ly.emp.service.IHubTeamResultOrderService;
import com.ly.emp.util.TeamValidatorUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制器-积分登记-汇总发布
 * @Author: luoy
 * @Date:   2023-05-25
 */
@Tag(name = "hub-积分登记-工单工分")
@Slf4j
@RestController
@RequestMapping("/emp/hubTeamResultOrder")
@RequiredArgsConstructor
public class HubTeamResultOrderController {

	private final IHubTeamResultOrderService hubTeamResultOrderService;


	/**
	 * 工分登记
	 *
	 * @param entity
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Operation(summary = "工分登记-分页")
	@GetMapping( "/getPage")
	public R<PageResult> getPageList(TeamResultOrder entity,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		return hubTeamResultOrderService.getPage(entity, new Page<>(pageNo, pageSize));
	}

	/**
	 * 考核工作类型列表
	 *
	 * @param entity
	 * @return
	 */
	@Operation(summary = "获取工作类型列表")
	@PostMapping( "/getWorkList")
	public R<List<HubTreeVO>> getWorkList(@RequestBody HubTeamWork entity) {
		return hubTeamResultOrderService.getWorkList(entity);
	}


	/**
	 * 工作类型详细信息
	 *
	 * @param id
	 * @return
	 */
	@Operation(summary = "获取工作类型详情")
	@GetMapping("/getWorkDetail")
	public R<HubTeamWorkVO> getCodeDetail(String id) {
		return hubTeamResultOrderService.getWorkDetail(id);
	}

	@Operation(summary = "工分登记-保存-按事/按人")
	@PostMapping("/save")
	public R save(@Validated @RequestBody TeamResultOrderDTO dto) {
		return hubTeamResultOrderService.save(dto);
	}

	@Operation(summary = "获取工分登记详情")
	@GetMapping("/getWorkCheckInDetail")
	public R<TeamResultOrderDTO> getWorkCheckInDetail(String seqKey) {
		return hubTeamResultOrderService.getWorkCheckInDetail(seqKey);
	}

	@Operation(summary = "获取工分登记详情-二维表")
	@PostMapping("/getWorkCheckInDetailForTwoDimensional")
	public R<TeamResultOrderDTO> getWorkCheckInDetailForTwoDimensional(@Validated @RequestBody HubTeamDTO dto) {
		return hubTeamResultOrderService.getWorkCheckInDetailForTwoDimensional(dto);
	}

	@Operation(summary = "删除工单")
	@PostMapping("/delete")
	public R deleteResultOrder(String seqKeys){
		return hubTeamResultOrderService.deleteResultOrder(seqKeys);
	}

	// @Operation(summary = "二维表-评分")
	// @GetMapping("/getWorkDetail")
	// public R<HubTeamWorkVO> getCodeDetail(String code) {
	// 	return hubTeamResultOrderService.getWorkDetail(code);
	// }


	/**
	 * 导出
	 *
	 * @param response
	 */
	@Operation(summary = "导出")
	@GetMapping("/exportXls")
	public void export(HttpServletResponse response, HubTeamDTO dto) {
		//验证必传参数
		TeamValidatorUtils.validate(dto);
		TeamResultOrderDetail entity = BeanUtil.toBean(dto, TeamResultOrderDetail.class);
		R r = hubTeamResultOrderService.getList(entity);
		List<TeamResultOrderDetail> list = (List<TeamResultOrderDetail>) r.getResult();
		List<HubTeamResultOrderExcelVO>  voList = new ArrayList<>();
		if(CollUtil.isNotEmpty(list)){
			list.stream().forEach(n -> voList.add(BeanUtil.copyProperties(n, HubTeamResultOrderExcelVO.class)));
		}
		ExcelUtil.exportExcel(voList, "sheet1", HubTeamResultOrderExcelVO.class, response);
	}

	/**
	 * 导入
	 *
	 * @return
	 */
	@Operation(summary = "导入")
	@PostMapping("/importExcel")
	public R importWork(MultipartFile file, boolean updateSupport, HubTeamDTO dto) throws Exception {
		//验证必传参数
		TeamValidatorUtils.validate(dto);
		ExcelResult<HubTeamResultOrderExcelVO> result =
				ExcelUtil.importExcel(file.getInputStream(), HubTeamResultOrderExcelVO.class,new HubTeamResultOrderListener(updateSupport,dto));
		return R.ok(true,result.getAnalysis());
	}

}
